using System.Collections;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using I2.Loc;
using UnityEngine;

public class BCWFlagInterface : MonoBehaviour
{
	public UIWidget container;

	public UISprite barFill;

	public UILabel notification;

	public UILabel capturePoints;

	public bool onInterfaceAnimation;

	private Tweener tween;

	private void Awake()
	{
		if (GameController.instance != null && GameController.instance.curTypeGame != TypeGame.tanks)
		{
			container.gameObject.SetActive(false);
		}
	}

	private void OnEnable()
	{
		if (GameController.instance != null && GameController.instance.IsFlagGameType())
		{
			StartCoroutine(OurPlayerCaptureFlag());
		}
	}

	private void OnDisable()
	{
		if (GameController.instance != null && GameController.instance.IsFlagGameType())
		{
			StopCoroutine(OurPlayerCaptureFlag());
		}
	}

	private void OnDestroy()
	{
		if (GameController.instance != null && GameController.instance.IsFlagGameType())
		{
			StopCoroutine(OurPlayerCaptureFlag());
		}
	}

	private IEnumerator OurPlayerCaptureFlag()
	{
		while (true)
		{
			if (BCWFlagController.instance != null && BCWFlagController.instance.ourPlayerCaptureFlag != null && TDMController.instance.CountTeam(PunTeams.Team.blue) > 0 && TDMController.instance.CountTeam(PunTeams.Team.red) > 0)
			{
				container.gameObject.SetActive(true);
				bool myTeamCapture = (TDMController.myTeam == PunTeams.Team.red && BCWFlagController.instance.ourPlayerCaptureFlag.redTeamScore > 0) || (TDMController.myTeam == PunTeams.Team.blue && BCWFlagController.instance.ourPlayerCaptureFlag.blueTeamScore > 0);
				switch (BCWFlagController.instance.ourPlayerCaptureFlag.captureMessage)
				{
				case CaptureMsg.Interception:
					if (tween != null)
					{
						HOTween.Kill(tween);
						tween = null;
						container.alpha = 1f;
					}
					barFill.color = Color.green;
					notification.text = ScriptLocalization.Get("Game_Common/tank_Interception_flag");
					break;
				case CaptureMsg.Captured:
					if (tween != null)
					{
						HOTween.Kill(tween);
						tween = null;
						container.alpha = 1f;
					}
					barFill.color = Color.green;
					notification.text = ScriptLocalization.Get("Game_Common/tank_your_flag");
					break;
				case CaptureMsg.Draw:
					if (tween == null)
					{
						tween = HOTween.To(container, 1f, new TweenParms().Prop("alpha", 0.5f).Loops(-1, LoopType.Yoyo));
					}
					notification.text = ScriptLocalization.Get("Game_Common/tank_dispute_flag");
					break;
				case CaptureMsg.None:
					container.gameObject.SetActive(false);
					break;
				}
				int currentPoints = ((BCWFlagController.instance.ourPlayerCaptureFlag.redTeamScore <= 0) ? BCWFlagController.instance.ourPlayerCaptureFlag.blueTeamScore : BCWFlagController.instance.ourPlayerCaptureFlag.redTeamScore);
				barFill.fillAmount = (float)currentPoints / BCWFlagController.instance.ourPlayerCaptureFlag.maxCountForCapture;
				capturePoints.text = currentPoints.ToString();
			}
			else
			{
				container.gameObject.SetActive(false);
			}
			yield return new WaitForSeconds(1f);
		}
	}

	private void Update()
	{
		if (GameController.instance != null && GameController.instance.curTypeGame == TypeGame.tanks)
		{
			InterfaceForSingleFlag();
		}
	}

	private void InterfaceForSingleFlag()
	{
		if (BCWFlagManager.instance != null)
		{
			container.gameObject.SetActive((BCWFlagManager.instance.redTeamScore > 0 || BCWFlagManager.instance.blueTeamScore > 0 || BCWFlagManager.instance.twoTeamsInRadius) && PVPManager.instance.gameIsStarted);
			bool flag = (TDMController.myTeam == PunTeams.Team.red && BCWFlagManager.instance.redTeamScore > 0) || (TDMController.myTeam == PunTeams.Team.blue && BCWFlagManager.instance.blueTeamScore > 0);
			if (BCWFlagManager.instance.twoTeamsInRadius)
			{
				if (tween == null)
				{
					tween = HOTween.To(container, 1f, new TweenParms().Prop("alpha", 0.5f).Loops(-1, LoopType.Yoyo));
				}
				notification.text = ScriptLocalization.Get("Game_Common/tank_dispute_flag");
			}
			else
			{
				if (tween != null)
				{
					HOTween.Kill(tween);
					tween = null;
					container.alpha = 1f;
				}
				if (flag)
				{
					barFill.color = Color.green;
					notification.text = ScriptLocalization.Get("Game_Common/tank_your_flag");
				}
				else
				{
					barFill.color = Color.red;
					notification.text = ScriptLocalization.Get("Game_Common/tank_enemy_flag");
				}
			}
			int num = ((BCWFlagManager.instance.redTeamScore <= 0) ? BCWFlagManager.instance.blueTeamScore : BCWFlagManager.instance.redTeamScore);
			barFill.fillAmount = Mathf.Lerp(barFill.fillAmount, (float)num / 100f, Time.deltaTime * 5f);
			capturePoints.text = num.ToString();
			if (num > 80)
			{
				if (!onInterfaceAnimation)
				{
					AnimationBar();
					onInterfaceAnimation = true;
				}
			}
			else
			{
				onInterfaceAnimation = false;
				StopAnimation();
			}
		}
		else
		{
			container.gameObject.SetActive(false);
		}
	}

	private void AnimationBar()
	{
		Color white = Color.white;
		if (!(BCWFlagController.instance != null) || !(BCWFlagController.instance.ourPlayerCaptureFlag != null))
		{
			return;
		}
		white = (((TDMController.myTeam != PunTeams.Team.red || BCWFlagController.instance.ourPlayerCaptureFlag.redTeamScore <= 0) && (TDMController.myTeam != PunTeams.Team.blue || BCWFlagController.instance.ourPlayerCaptureFlag.blueTeamScore <= 0)) ? Color.red : Color.blue);
		HOTween.To(capturePoints.transform, 0.5f, new TweenParms().Prop("localScale", Vector3.one * 4f));
		HOTween.To(capturePoints, 0.5f, new TweenParms().Prop("color", white).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			HOTween.To(capturePoints.transform, 0.5f, new TweenParms().Prop("localScale", Vector3.one));
			HOTween.To(capturePoints, 0.5f, new TweenParms().Prop("color", Color.white).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				AnimationBar();
			}));
		}));
	}

	private void StopAnimation()
	{
		HOTween.Kill(capturePoints);
		capturePoints.transform.localScale = Vector3.one;
		capturePoints.color = Color.white;
	}
}
